package com.getpebble.android.jskit;

import com.getpebble.android.common.core.trace.Trace;
import java.io.File;
import javax.activation.MimetypesFileTypeMap;

public class HttpUtils {

    protected static class MimeTypesFileMapRef {
        public static MimetypesFileTypeMap REF = make();

        protected static MimetypesFileTypeMap make() {
            try {
                MimetypesFileTypeMap rv = new MimetypesFileTypeMap();
                rv.addMimeTypes("application/json json JSON");
                rv.addMimeTypes("application/javascript js JS Js javascript JAVASCRIPT Javascript");
                rv.addMimeTypes("text/css css Css CSS");
                return rv;
            } catch (Exception e) {
                Trace.error("HttpUtils", "MimetypesFileTypeMap.make", e);
                try {
                    return new MimetypesFileTypeMap();
                } catch (Exception e2) {
                    Trace.error("HttpUtils", "MimetypesFileTypeMap.make", e2);
                    return null;
                }
            }
        }
    }

    public static String getMimeType(String filenameWithExtension) {
        try {
            MimetypesFileTypeMap mftm = mimeTypesFileMapStaticRef();
            int realExtensionDot = filenameWithExtension.lastIndexOf(".");
            return mftm.getContentType(new File(filenameWithExtension.substring(0, filenameWithExtension.indexOf(".")) + filenameWithExtension.substring(filenameWithExtension.lastIndexOf("."))));
        } catch (Exception e) {
            Trace.error("HttpUtils", "getMimeType", e);
            return "";
        }
    }

    public static MimetypesFileTypeMap mimeTypesFileMapStaticRef() {
        return MimeTypesFileMapRef.REF;
    }
}
